import java.io.File;
import java.util.List;

import javax.swing.JFileChooser;
import cytoscape.*;
import cytoscape.task.ui.JTaskConfig;
import cytoscape.task.util.TaskManager;



/**
 * EnrichmentPanel.java
 * This class generates the GUI of the annot plugin.
 * Created on 10/09/2009, 10:06:22
 */

public class EnrichmentPanel extends javax.swing.JPanel {

	JTaskConfig _config;
	
	/** Creates new form EnrichmentPanel */
	public EnrichmentPanel() {
		initComponents();
		GUIException.setDesktop(Cytoscape.getDesktop());
		_config = new JTaskConfig();
        _config.displayCancelButton(true);
        _config.displayCloseButton(false);
        _config.setModal(false);
        _config.displayStatus(true);
        
	}

	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
	private void initComponents() {

		buttonGroup1 = new javax.swing.ButtonGroup();
		jPanel1 = new javax.swing.JPanel();
		jToggleButton1 = new javax.swing.JToggleButton();
		jPanel2 = new javax.swing.JPanel();
		jLabel2 = new javax.swing.JLabel();
		outputField = new javax.swing.JTextField();
		browseButton = new javax.swing.JButton();
		jLabel1 = new javax.swing.JLabel();
		helpButton = new javax.swing.JButton();
		jPanel4 = new javax.swing.JPanel();
		mclButton = new javax.swing.JRadioButton();
		blastButton = new javax.swing.JRadioButton();
		jPanel3 = new javax.swing.JPanel();
		jLabel3 = new javax.swing.JLabel();
		enrichField = new javax.swing.JTextField();
		jLabel6 = new javax.swing.JLabel();
		alphaField = new javax.swing.JTextField();
		annotateButton = new javax.swing.JToggleButton();

		buttonGroup1.add(mclButton);
		buttonGroup1.add(blastButton);

		jToggleButton1.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
		jToggleButton1.setForeground(new java.awt.Color(0, 153, 255));
		jToggleButton1.setText("GO Clusters");

		jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(""));
		jPanel2.setPreferredSize(new java.awt.Dimension(1205, 66));

		jLabel2.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
		jLabel2.setText("Output file");

		outputField.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
		outputField.setName("outputField"); // NOI18N

		browseButton.setFont(new java.awt.Font("Tahoma", 1, 8)); // NOI18N
		browseButton.setText("Browse");

		javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
		jPanel2.setLayout(jPanel2Layout);
		jPanel2Layout.setHorizontalGroup(
				jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(jPanel2Layout.createSequentialGroup()
						.addContainerGap()
						.addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)
						.addGap(8, 8, 8)
						.addComponent(outputField, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
						.addComponent(browseButton, javax.swing.GroupLayout.DEFAULT_SIZE, 61, Short.MAX_VALUE)
						.addContainerGap())
		);
		
		jPanel2Layout.setVerticalGroup(
				jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
						.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
						.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
								.addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
								.addComponent(browseButton, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
								.addComponent(outputField, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE))
								.addContainerGap())
		);

		jLabel1.setBackground(new java.awt.Color(0, 153, 255));
		jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
		jLabel1.setText("GO Enriched Clusters");

		helpButton.setFont(new java.awt.Font("Tahoma", 1, 10)); // NOI18N
		helpButton.setText("Help");
		

		jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Clustering Algorithm", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 12))); // NOI18N

		mclButton.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
		
		mclButton.setText("Markov Clustering");

		blastButton.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
		blastButton.setText("NetworkBlast");
		blastButton.setSelected(true);
		
		javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
		jPanel4.setLayout(jPanel4Layout);
		jPanel4Layout.setHorizontalGroup(
				jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(jPanel4Layout.createSequentialGroup()
						.addContainerGap()
						.addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
								.addComponent(blastButton)
								.addComponent(mclButton))
								.addContainerGap(130, Short.MAX_VALUE))
		);
		jPanel4Layout.setVerticalGroup(
				jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(jPanel4Layout.createSequentialGroup()
						.addComponent(blastButton)
						.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
						.addComponent(mclButton)
						.addContainerGap(10, Short.MAX_VALUE))
		);

		javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
		jPanel1.setLayout(jPanel1Layout);
		jPanel1Layout.setHorizontalGroup(
				jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(jPanel1Layout.createSequentialGroup()
						.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
								.addGroup(jPanel1Layout.createSequentialGroup()
										.addGap(1101, 1101, 1101)
										.addComponent(jToggleButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 114, javax.swing.GroupLayout.PREFERRED_SIZE))
										.addGroup(jPanel1Layout.createSequentialGroup()
												.addContainerGap()
												.addComponent(jLabel1))
												.addGroup(jPanel1Layout.createSequentialGroup()
														.addGap(205, 205, 205)
														.addComponent(helpButton))
														.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
																.addComponent(jPanel4, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
																.addComponent(jPanel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 268, Short.MAX_VALUE)))
																.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
		);
		jPanel1Layout.setVerticalGroup(
				jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(jPanel1Layout.createSequentialGroup()
						.addContainerGap()
						.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
						.addGap(13, 13, 13)
						.addComponent(helpButton)
						.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
						.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE)
						.addGap(172, 172, 172)
						.addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
						.addGap(137, 137, 137)
						.addComponent(jToggleButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
						.addContainerGap())
		);

		jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Clustering parameters", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 12))); // NOI18N
		jPanel3.setName(""); // NOI18N

		jLabel3.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
		jLabel3.setText("Enrichement cutoff [0..1]");

		enrichField.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
		enrichField.setText("0.05");
		enrichField.setName("enrichedField"); // NOI18N

		jLabel6.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
		jLabel6.setText("Alpha [0..1]");

		alphaField.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
		alphaField.setText("0.75");
		alphaField.setName("alphaField"); // NOI18N

		javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
		jPanel3.setLayout(jPanel3Layout);
		jPanel3Layout.setHorizontalGroup(
				jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(jPanel3Layout.createSequentialGroup()
						.addContainerGap()
						.addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
								.addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 158, javax.swing.GroupLayout.PREFERRED_SIZE)
								.addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE))
								.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
										.addComponent(alphaField)
										.addComponent(enrichField, javax.swing.GroupLayout.DEFAULT_SIZE, 42, Short.MAX_VALUE))
										.addContainerGap(38, Short.MAX_VALUE))
		);
		jPanel3Layout.setVerticalGroup(
				jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
						.addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
								.addComponent(enrichField, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)
								.addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE))
								.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
										.addComponent(alphaField, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
										.addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE))
										.addContainerGap(22, Short.MAX_VALUE))
		);

		annotateButton.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
		annotateButton.setForeground(new java.awt.Color(0, 153, 255));
		annotateButton.setText("GO Clusters");
		
			
		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
		this.setLayout(layout);
		layout.setHorizontalGroup(
				layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(layout.createSequentialGroup()
						.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
								.addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
								.addGroup(layout.createSequentialGroup()
										.addGap(79, 79, 79)
										.addComponent(annotateButton)))
										.addContainerGap(14, Short.MAX_VALUE))
										.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
												.addGroup(layout.createSequentialGroup()
														.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 268, javax.swing.GroupLayout.PREFERRED_SIZE)
														.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
		);
		layout.setVerticalGroup(
				layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(layout.createSequentialGroup()
						.addGap(196, 196, 196)
						.addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 92, Short.MAX_VALUE)
						.addComponent(annotateButton, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
						.addContainerGap())
						.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
								.addGroup(layout.createSequentialGroup()
										.addContainerGap()
										.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 434, javax.swing.GroupLayout.PREFERRED_SIZE)
										.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
		);
		// Action Listeners.
		browseButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				browseButtonActionPerformed(evt);
			}
		});
		helpButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				helpButtonActionPerformed(evt);
			}
		});
		annotateButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				annotateButtonActionPerformed(evt);
			}
		});
		
	}// </editor-fold>//GEN-END:initComponents

	/*
	* Activates the help panel once the user presses on
	* the help button.
	*/
	private void helpButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_helpButtonActionPerformed
		HelpFrame help = new HelpFrame();
		help.setVisible(true);
	}//GEN-LAST:event_helpButtonActionPerformed


	/**
	 * Does all the actions that results from pressing on the annotation button
	 * such as generating a new network in cytoscape.
	 * @param evt the even of pressing on the Annot button.
	 */
	//@SuppressWarnings("unchecked")
	private void annotateButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_annotateButtonActionPerformed
		// Check the alguments
		double cutoff = -1;
		double alpha;
		try {
			cutoff = Double.parseDouble(enrichField.getText());

		} catch (Exception e) {
			GUIException.displayErrorMessage("Illegal cutoff\nPlease enter a number in the range (0,1)");
			return;
		}
		if ((cutoff > 1) ||  (cutoff < 0)) {
			GUIException.displayErrorMessage("Illegal cutoff\nPlease enter a number in the range (0,1)");
			return;  
		}
		// check output file
		String outputFile = outputField.getText();
		if ( (outputFile==null)   || (outputFile.equals(""))) {
			GUIException.displayErrorMessage("Empty output file name!!!\nPlease select a file to save the clustering results");
			return;
		}
		try {
			alpha = Double.parseDouble(alphaField.getText());
		} catch (Exception e) {
			GUIException.displayErrorMessage("Illegal Alpha\nPlease enter a number in the range [0,1]");
			return;
		}
		if ((alpha > 1) ||  (alpha < 0)) {
			GUIException.displayErrorMessage("Illegal Alpha\nPlease enter a number in the range [0,1]");
			return;  
		}
		int algorithm = PluginCore.MCL;
		// Get the chosen clustering algorithm.
		if (blastButton.isSelected()) {
			algorithm = PluginCore.NETWORK_BLAST;
		}
			
 		CyNetwork cyNet = Cytoscape.getCurrentNetwork();
		List<CyNode> nodes = cyNet.nodesList();
		List<CyEdge> edges = cyNet.edgesList();

		if ((nodes.size() == 0) && (edges.size() == 0)) {
			GUIException.displayErrorMessage("Please upload a network into cytoscape");
			return;
		}
		PluginCore pluginCore = new PluginCore(outputFile, cutoff,alpha, algorithm );
		boolean success = TaskManager.executeTask(pluginCore, _config);
		//System.out.println("succeded to fulfil task");
		//pluginCore.run();
	}//GEN-LAST:event_annotateButtonActionPerformed



	// enables the user to choose the file on which to save the results.
	private void browseButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_browseButtonActionPerformed
		JFileChooser fc = new JFileChooser();
		fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
		int returnVal = fc.showSaveDialog(this);
		if (returnVal == JFileChooser.APPROVE_OPTION) {
			File file = fc.getSelectedFile();
			outputField.setText(file.getAbsolutePath());
		}
	}//GEN-LAST:event_browseButtonActionPerformed




	// Variables declaration - do not modify//GEN-BEGIN:variables
	private javax.swing.JTextField alphaField;
	private javax.swing.JToggleButton annotateButton;
	private javax.swing.JRadioButton blastButton;
	private javax.swing.ButtonGroup buttonGroup1;
	private javax.swing.JTextField enrichField;
	private javax.swing.JButton helpButton;
	private javax.swing.JButton browseButton;
	private javax.swing.JLabel jLabel1;
	private javax.swing.JLabel jLabel2;
	private javax.swing.JLabel jLabel3;
	private javax.swing.JLabel jLabel6;
	private javax.swing.JPanel jPanel1;
	private javax.swing.JPanel jPanel2;
	private javax.swing.JPanel jPanel3;
	private javax.swing.JPanel jPanel4;
	private javax.swing.JToggleButton jToggleButton1;
	private javax.swing.JRadioButton mclButton;
	private javax.swing.JTextField outputField;
	// End of variables declaration//GEN-END:variables

}
